﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace System.Windows.Media.DirectShow.Filters
{
    public abstract class BasePullPin : BaseOutputPin, IAsyncReader
    {

        public BasePullPin(IBaseFilter filter)
            : base(filter)
        {

        }

        #region IAsyncReader Members

        void IAsyncReader.Request(long startMediaTime, long endMediaTime, object state)
        {
            this.QueueWork(s =>
            {
                Request(startMediaTime, endMediaTime, state);
            });
        }

        IMemAllocator IAsyncReader.RequestAllocator(IMemAllocator preferred, AllocatorProperties properties)
        {
            if (preferred != null)
                this.Allocator = preferred;
            else
                this.Allocator = base.DecideAllocator(this.ConnectedPin as IMemInputPin);

            return this.Allocator;
        }

        /// <summary>
        /// Called by input pin to request new sample. You must ensure that you call RequestCompleted always on the same thread.
        /// </summary>
        /// <param name="state"></param>
        protected abstract void Request(long startMediaTime, long endMediaTime, object state);

        public event EventHandler<ReaderAsyncCompletedEventArgs> RequestCompleted;

        /// <summary>
        /// You must ensure that you call RequestCompleted always on the same thread.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnRequestCompleted(ReaderAsyncCompletedEventArgs e)
        {
            if (RequestCompleted != null)
                RequestCompleted(this, e);
        }

        #endregion

        protected internal void Seek(long position)
        {
        }

        protected override IMemAllocator DecideAllocator(IMemInputPin inputPin)
        {
            // Allocator should be already decided by input pin
            throw new InvalidOperationException("Not supported because this is a push pin");
        }
    }
}
